--TEST--
filters
--SKIPIF--
--FILE--
<?php
require_once dirname(__FILE__) . '/../common/config.php';

$mysql = get_mysql_connection();

init_mysql_testdb($mysql);

$table = 'hstesttbl';
$tablesize = 10;
$sql = sprintf(
    'CREATE TABLE %s ( ' .
    'k1 varchar(30) NOT NULL, ' .
    'k2 varchar(30) NOT NULL, ' .
    'v1 int NOT NULL, ' .
    'v2 int NOT NULL, ' .
    'PRIMARY KEY (k1, k2)) ' .
    'Engine = innodb',
    mysql_real_escape_string($table));
if (!mysql_query($sql, $mysql))
{
    die(mysql_error());
}

$valmap = array();

for ($i = 0; $i < $tablesize; $i++)
{
    for ($j = 0; $j < $tablesize; $j++)
    {
        $k1 = 'k1_' . $i;
        $k2 = 'k2_' . $j;
        $v1 = $i + $j;
        $v2 = $i * $j;

        $sql = sprintf(
            'INSERT INTO ' . $table . ' VALUES (\'%s\', \'%s\', \'%s\', \'%s\')',
            mysql_real_escape_string($k1),
            mysql_real_escape_string($k2),
            mysql_real_escape_string($v1),
            mysql_real_escape_string($v2));
        if (!mysql_query($sql, $mysql))
        {
            break;
        }
    }
}


$hs = new HandlerSocket(MYSQL_HOST, MYSQL_HANDLERSOCKET_PORT_WR);
if (!($hs->openIndex(1, MYSQL_DBNAME, $table, '', 'k1,k2,v1,v2', 'k2')))
{
    die();
}
if (!($hs->openIndex(2, MYSQL_DBNAME, $table, '', 'k1,k2,v1,v2', 'k1,k2,v1,v2')))
{
    die();
}
if (!($hs->openIndex(3, MYSQL_DBNAME, $table, '', 'v1', 'k1,v2')))
{
    die();
}

echo 'VAL', PHP_EOL;
$retval = $hs->executeMulti(
    array(
        array(1, '>=', array('', ''), 1000, 0),
    )
);
_dump($retval);

// select k1, k2, v1, v2 ... where (k1, k2) >= ('', '') and k2 = 'k2_5'
echo 'FILTER', PHP_EOL;
$retval = $hs->executeSingle(
    1, '>=', array('', ''), 1000, 0, null, null,
    array(array('F', '=', 0, 'k2_5')));
_dump($retval, false);

// same as above
echo 'FILTER', PHP_EOL;
$retval = $hs->executeMulti(
    array(
        array(1, '>=', array('', ''), 1000, 0, null, null,
              array(array('F', '=', 0, 'k2_5')))
    )
);
_dump($retval);

// select k1, k2, v1, v2 ... where (k1, k2) >= ('', '') and v1 = 3
echo 'FILTER', PHP_EOL;
$retval = $hs->executeMulti(
    array(
        array(2, '>=', array('', ''), 1000, 0, null, null,
              array(array('F', '=', 2, 3)))
    )
);
_dump($retval);

// select k1, k2, v1, v2 ... where (k1, k2) >= ('k1_1', '') and k1 <= 'k1_2'
echo 'FILTER', PHP_EOL;
$retval = $hs->executeMulti(
    array(
        array(2, '>=', array('k1_1', ''), 1000, 0, null, null,
              array(array('W', '<=', 0, 'k1_2')))
    )
);
_dump($retval);

// select k1, k2, v1, v2 ... where (k1, k2) >= ('k1_1', '') and k1 <= 'k1_2'
//   and v2 >= 10
echo 'FILTER', PHP_EOL;
$retval = $hs->executeMulti(
    array(
        array(2, '>=', array('k1_1', ''), 1000, 0, null, null,
              array(array('W', '<=', 0, 'k1_2'), array('F', '>=', 3, 10)))
    )
);
_dump($retval);

// update ... set v2 = -1 where (k1, k2) >= ('k1_1', '') and v2 = 10
echo 'FILTER', PHP_EOL;
$retval = $hs->executeMulti(
    array(
        array(3, '>=', array('k1_1', ''), 1000, 0, 'U', array(-1),
              array(array('F', '=', 1, 10)))
    )
);
_dump($retval);

echo 'VAL', PHP_EOL;
$retval = $hs->executeMulti(
    array(
        array(1, '>=', array('', ''), 1000, 0)
    )
);
_dump($retval);

mysql_close($mysql);

function _dump($data = array(), $multi = true)
{
    foreach ($data as $code => $value)
    {
        if ($multi)
        {
            foreach ($value as $key => $val)
            {
                if (is_array($val))
                {
                    foreach ($val as $v)
                    {
                        echo '[', $v, ']';
                    }
                }
                else
                {
                    echo '[', $val, ']';
                }
                echo PHP_EOL;
            }
        }
        else
        {
            foreach ($value as $key => $val)
            {
                echo '[', $val, ']';
            }
            echo PHP_EOL;
        }
    }
}
--EXPECT--
VAL
[k1_0][k2_0][0][0]
[k1_0][k2_1][1][0]
[k1_0][k2_2][2][0]
[k1_0][k2_3][3][0]
[k1_0][k2_4][4][0]
[k1_0][k2_5][5][0]
[k1_0][k2_6][6][0]
[k1_0][k2_7][7][0]
[k1_0][k2_8][8][0]
[k1_0][k2_9][9][0]
[k1_1][k2_0][1][0]
[k1_1][k2_1][2][1]
[k1_1][k2_2][3][2]
[k1_1][k2_3][4][3]
[k1_1][k2_4][5][4]
[k1_1][k2_5][6][5]
[k1_1][k2_6][7][6]
[k1_1][k2_7][8][7]
[k1_1][k2_8][9][8]
[k1_1][k2_9][10][9]
[k1_2][k2_0][2][0]
[k1_2][k2_1][3][2]
[k1_2][k2_2][4][4]
[k1_2][k2_3][5][6]
[k1_2][k2_4][6][8]
[k1_2][k2_5][7][10]
[k1_2][k2_6][8][12]
[k1_2][k2_7][9][14]
[k1_2][k2_8][10][16]
[k1_2][k2_9][11][18]
[k1_3][k2_0][3][0]
[k1_3][k2_1][4][3]
[k1_3][k2_2][5][6]
[k1_3][k2_3][6][9]
[k1_3][k2_4][7][12]
[k1_3][k2_5][8][15]
[k1_3][k2_6][9][18]
[k1_3][k2_7][10][21]
[k1_3][k2_8][11][24]
[k1_3][k2_9][12][27]
[k1_4][k2_0][4][0]
[k1_4][k2_1][5][4]
[k1_4][k2_2][6][8]
[k1_4][k2_3][7][12]
[k1_4][k2_4][8][16]
[k1_4][k2_5][9][20]
[k1_4][k2_6][10][24]
[k1_4][k2_7][11][28]
[k1_4][k2_8][12][32]
[k1_4][k2_9][13][36]
[k1_5][k2_0][5][0]
[k1_5][k2_1][6][5]
[k1_5][k2_2][7][10]
[k1_5][k2_3][8][15]
[k1_5][k2_4][9][20]
[k1_5][k2_5][10][25]
[k1_5][k2_6][11][30]
[k1_5][k2_7][12][35]
[k1_5][k2_8][13][40]
[k1_5][k2_9][14][45]
[k1_6][k2_0][6][0]
[k1_6][k2_1][7][6]
[k1_6][k2_2][8][12]
[k1_6][k2_3][9][18]
[k1_6][k2_4][10][24]
[k1_6][k2_5][11][30]
[k1_6][k2_6][12][36]
[k1_6][k2_7][13][42]
[k1_6][k2_8][14][48]
[k1_6][k2_9][15][54]
[k1_7][k2_0][7][0]
[k1_7][k2_1][8][7]
[k1_7][k2_2][9][14]
[k1_7][k2_3][10][21]
[k1_7][k2_4][11][28]
[k1_7][k2_5][12][35]
[k1_7][k2_6][13][42]
[k1_7][k2_7][14][49]
[k1_7][k2_8][15][56]
[k1_7][k2_9][16][63]
[k1_8][k2_0][8][0]
[k1_8][k2_1][9][8]
[k1_8][k2_2][10][16]
[k1_8][k2_3][11][24]
[k1_8][k2_4][12][32]
[k1_8][k2_5][13][40]
[k1_8][k2_6][14][48]
[k1_8][k2_7][15][56]
[k1_8][k2_8][16][64]
[k1_8][k2_9][17][72]
[k1_9][k2_0][9][0]
[k1_9][k2_1][10][9]
[k1_9][k2_2][11][18]
[k1_9][k2_3][12][27]
[k1_9][k2_4][13][36]
[k1_9][k2_5][14][45]
[k1_9][k2_6][15][54]
[k1_9][k2_7][16][63]
[k1_9][k2_8][17][72]
[k1_9][k2_9][18][81]
FILTER
[k1_0][k2_5][5][0]
[k1_1][k2_5][6][5]
[k1_2][k2_5][7][10]
[k1_3][k2_5][8][15]
[k1_4][k2_5][9][20]
[k1_5][k2_5][10][25]
[k1_6][k2_5][11][30]
[k1_7][k2_5][12][35]
[k1_8][k2_5][13][40]
[k1_9][k2_5][14][45]
FILTER
[k1_0][k2_5][5][0]
[k1_1][k2_5][6][5]
[k1_2][k2_5][7][10]
[k1_3][k2_5][8][15]
[k1_4][k2_5][9][20]
[k1_5][k2_5][10][25]
[k1_6][k2_5][11][30]
[k1_7][k2_5][12][35]
[k1_8][k2_5][13][40]
[k1_9][k2_5][14][45]
FILTER
[k1_0][k2_3][3][0]
[k1_1][k2_2][3][2]
[k1_2][k2_1][3][2]
[k1_3][k2_0][3][0]
FILTER
[k1_1][k2_0][1][0]
[k1_1][k2_1][2][1]
[k1_1][k2_2][3][2]
[k1_1][k2_3][4][3]
[k1_1][k2_4][5][4]
[k1_1][k2_5][6][5]
[k1_1][k2_6][7][6]
[k1_1][k2_7][8][7]
[k1_1][k2_8][9][8]
[k1_1][k2_9][10][9]
[k1_2][k2_0][2][0]
[k1_2][k2_1][3][2]
[k1_2][k2_2][4][4]
[k1_2][k2_3][5][6]
[k1_2][k2_4][6][8]
[k1_2][k2_5][7][10]
[k1_2][k2_6][8][12]
[k1_2][k2_7][9][14]
[k1_2][k2_8][10][16]
[k1_2][k2_9][11][18]
FILTER
[k1_2][k2_5][7][10]
[k1_2][k2_6][8][12]
[k1_2][k2_7][9][14]
[k1_2][k2_8][10][16]
[k1_2][k2_9][11][18]
FILTER
[2]
VAL
[k1_0][k2_0][0][0]
[k1_0][k2_1][1][0]
[k1_0][k2_2][2][0]
[k1_0][k2_3][3][0]
[k1_0][k2_4][4][0]
[k1_0][k2_5][5][0]
[k1_0][k2_6][6][0]
[k1_0][k2_7][7][0]
[k1_0][k2_8][8][0]
[k1_0][k2_9][9][0]
[k1_1][k2_0][1][0]
[k1_1][k2_1][2][1]
[k1_1][k2_2][3][2]
[k1_1][k2_3][4][3]
[k1_1][k2_4][5][4]
[k1_1][k2_5][6][5]
[k1_1][k2_6][7][6]
[k1_1][k2_7][8][7]
[k1_1][k2_8][9][8]
[k1_1][k2_9][10][9]
[k1_2][k2_0][2][0]
[k1_2][k2_1][3][2]
[k1_2][k2_2][4][4]
[k1_2][k2_3][5][6]
[k1_2][k2_4][6][8]
[k1_2][k2_5][-1][10]
[k1_2][k2_6][8][12]
[k1_2][k2_7][9][14]
[k1_2][k2_8][10][16]
[k1_2][k2_9][11][18]
[k1_3][k2_0][3][0]
[k1_3][k2_1][4][3]
[k1_3][k2_2][5][6]
[k1_3][k2_3][6][9]
[k1_3][k2_4][7][12]
[k1_3][k2_5][8][15]
[k1_3][k2_6][9][18]
[k1_3][k2_7][10][21]
[k1_3][k2_8][11][24]
[k1_3][k2_9][12][27]
[k1_4][k2_0][4][0]
[k1_4][k2_1][5][4]
[k1_4][k2_2][6][8]
[k1_4][k2_3][7][12]
[k1_4][k2_4][8][16]
[k1_4][k2_5][9][20]
[k1_4][k2_6][10][24]
[k1_4][k2_7][11][28]
[k1_4][k2_8][12][32]
[k1_4][k2_9][13][36]
[k1_5][k2_0][5][0]
[k1_5][k2_1][6][5]
[k1_5][k2_2][-1][10]
[k1_5][k2_3][8][15]
[k1_5][k2_4][9][20]
[k1_5][k2_5][10][25]
[k1_5][k2_6][11][30]
[k1_5][k2_7][12][35]
[k1_5][k2_8][13][40]
[k1_5][k2_9][14][45]
[k1_6][k2_0][6][0]
[k1_6][k2_1][7][6]
[k1_6][k2_2][8][12]
[k1_6][k2_3][9][18]
[k1_6][k2_4][10][24]
[k1_6][k2_5][11][30]
[k1_6][k2_6][12][36]
[k1_6][k2_7][13][42]
[k1_6][k2_8][14][48]
[k1_6][k2_9][15][54]
[k1_7][k2_0][7][0]
[k1_7][k2_1][8][7]
[k1_7][k2_2][9][14]
[k1_7][k2_3][10][21]
[k1_7][k2_4][11][28]
[k1_7][k2_5][12][35]
[k1_7][k2_6][13][42]
[k1_7][k2_7][14][49]
[k1_7][k2_8][15][56]
[k1_7][k2_9][16][63]
[k1_8][k2_0][8][0]
[k1_8][k2_1][9][8]
[k1_8][k2_2][10][16]
[k1_8][k2_3][11][24]
[k1_8][k2_4][12][32]
[k1_8][k2_5][13][40]
[k1_8][k2_6][14][48]
[k1_8][k2_7][15][56]
[k1_8][k2_8][16][64]
[k1_8][k2_9][17][72]
[k1_9][k2_0][9][0]
[k1_9][k2_1][10][9]
[k1_9][k2_2][11][18]
[k1_9][k2_3][12][27]
[k1_9][k2_4][13][36]
[k1_9][k2_5][14][45]
[k1_9][k2_6][15][54]
[k1_9][k2_7][16][63]
[k1_9][k2_8][17][72]
[k1_9][k2_9][18][81]
